package com.ruoyi.web.controller.pc.points;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


@RestController
@RequestMapping("/points/applyInfo")
public class ApplyInfoController extends BaseController {

    @Autowired
    private PoiApplyMapper poiApplyMapper;

    @Autowired
    private UcQuoTableMapper ucQuoTableMapper;

    @Autowired
    private PoiApplyImageMapper poiApplyImageMapper;

    @Autowired
    private PoiApplyItemMapper poiApplyItemMapper;

    @Autowired
    private UcMyFlowMapper ucMyFlowMapper;

    @RequestMapping("/searchApplyInfo/{applyId}")
    public AjaxResult searchApplyInfo(@PathVariable("applyId") Long applyId){

        QueryVo queryVo = new QueryVo();
        queryVo.setQId(applyId);
        PoiApply poiApply = poiApplyMapper.selectByApply(queryVo);

        queryVo = new QueryVo();
        queryVo.setQId(poiApply.getTableId());
        UcQuoTable ucQuoTable = ucQuoTableMapper.searchBayId(queryVo);
        List<UcQuoProject> ucQuoProjects = new ArrayList<>();
        for (UcQuoProject ucQuoProject : ucQuoTable.getUcQuoProjectList()) {
            double score = poiApply.getPoiApplyItemList().stream().filter(item -> {
                return item.getProjectId().equals(ucQuoProject.getId());
            }).mapToDouble(PoiApplyItem::getScore).sum();
            if (score != 0) {
                ucQuoProjects.add(ucQuoProject);
            }
        }
        poiApply.setUcQuoProjects(ucQuoProjects);
//        poiApply.setUcQuoProjects(ucQuoTable.getUcQuoProjectList());

        for (UcQuoProject ucQuoProject : poiApply.getUcQuoProjects()) {

            double score = poiApply.getPoiApplyItemList().stream().filter(item -> {
                return item.getProjectId().equals(ucQuoProject.getId());
            }).mapToDouble(PoiApplyItem::getScore).sum();
            ucQuoProject.setScore(score);

            for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {
                PoiApplyItem poiApplyItem = poiApply.getPoiApplyItemList().stream().filter(item -> {
                    return item.getItemId().equals(ucQuoProjectItem.getId());
                }).findFirst().orElse(null);

                if(poiApplyItem!=null){
                    ucQuoProjectItem.setNum(poiApplyItem.getNum());
                    ucQuoProjectItem.setScoreText(poiApplyItem.getScore());
                    ucQuoProjectItem.setContent(poiApplyItem.getContent());
                }
            }

        }


        QueryWrapper<PoiApplyImage> qwPoiApplyImage=new QueryWrapper<>();
        qwPoiApplyImage.eq("apply_id",poiApply.getId());
        List<PoiApplyImage> poiApplyImageList = poiApplyImageMapper.selectList(qwPoiApplyImage);
        if(!StringUtils.isEmpty(poiApplyImageList)){
            poiApply.setImgList(new ArrayList<>());
            for (PoiApplyImage poiApplyImage : poiApplyImageList) {
                poiApply.getImgList().add(poiApplyImage.getImage());
            }
        }

        if (poiApply.getApplyStatus().equals(300)) {
            QueryWrapper<UcMyFlow> qwUcMyFlow=new QueryWrapper<>();
            qwUcMyFlow.eq("flow_id",poiApply.getId());
            qwUcMyFlow.eq("flow_status_start",200);
            qwUcMyFlow.eq("flow_status_end",300);
            UcMyFlow ucMyFlow = ucMyFlowMapper.selectOne(qwUcMyFlow);
            if (Objects.nonNull(ucMyFlow.getNotes())) {
                poiApply.setRemark(ucMyFlow.getNotes());
            } else {
                poiApply.setRemark("");
            }
        }

        return success(poiApply);
    }




    @GetMapping("/searchManagementInfo")
    public AjaxResult searchManagementInfo(QueryVo queryVo){

        PoiApply poiApply = poiApplyMapper.selectByApply1(queryVo);

        if(poiApply!=null){
            queryVo = new QueryVo();
            queryVo.setQId(poiApply.getTableId());
            UcQuoTable ucQuoTable = ucQuoTableMapper.searchBayId(queryVo);
            poiApply.setUcQuoProjects(ucQuoTable.getUcQuoProjectList());

            for (UcQuoProject ucQuoProject : poiApply.getUcQuoProjects()) {

                double score = poiApply.getPoiApplyItemList().stream().filter(item -> {
                    return item.getProjectId().equals(ucQuoProject.getId());
                }).mapToDouble(PoiApplyItem::getScore).sum();
                double score1 = poiApply.getPoiApplyItemList().stream().filter(item -> {
                    return item.getProjectId().equals(ucQuoProject.getId());
                }).mapToDouble(item -> Optional.ofNullable(item.getScore1()).orElse(0.0)).sum();
                double score2 = poiApply.getPoiApplyItemList().stream().filter(item -> {
                    return item.getProjectId().equals(ucQuoProject.getId());
                }).mapToDouble(item -> Optional.ofNullable(item.getScore2()).orElse(0.0)).sum();
                ucQuoProject.setScore(score);
                ucQuoProject.setScore1(score1);
                ucQuoProject.setScore2(score2);

                for (UcQuoProjectItem ucQuoProjectItem : ucQuoProject.getUcQuoProjectItems()) {
                    PoiApplyItem poiApplyItem = poiApply.getPoiApplyItemList().stream().filter(item -> {
                        return item.getItemId().equals(ucQuoProjectItem.getId());
                    }).findFirst().orElse(null);

                    if(poiApplyItem!=null){
                        ucQuoProjectItem.setNum(poiApplyItem.getNum());
                        ucQuoProjectItem.setScoreText(poiApplyItem.getScore());
                        ucQuoProjectItem.setContent(poiApplyItem.getContent());
                        ucQuoProjectItem.setNum1(poiApplyItem.getNum1());
                        ucQuoProjectItem.setNum2(poiApplyItem.getNum2());
                        ucQuoProjectItem.setScoreText1(poiApplyItem.getScore1());
                        ucQuoProjectItem.setScoreText2(poiApplyItem.getScore2());
                    }
                }

            }




            List<PoiApplyImage> poiApplyImageList = poiApply.getPoiApplyImages();
            if(!StringUtils.isEmpty(poiApplyImageList)){
                poiApply.setImgList(new ArrayList<>());
                for (PoiApplyImage poiApplyImage : poiApplyImageList) {
                    poiApply.getImgList().add(poiApplyImage.getImage());
                }
            }

        }


        return success(poiApply);
    }





}

